package com.dahua.messagesyncsh.service.Impl;

import com.dahua.messagesyncsh.criteria.FaultInfoQueryCriteria;
import com.dahua.messagesyncsh.entity.*;
import com.dahua.messagesyncsh.mapper.FaultMapper;
import com.dahua.messagesyncsh.mapper.PartGroupMapper;
import com.dahua.messagesyncsh.mapper.SensorDhMapper;
import com.dahua.messagesyncsh.pojo.EquipmentInfo;
import com.dahua.messagesyncsh.rabbitmq.pojo.EqStatusMessage;
import com.dahua.messagesyncsh.service.FaultInfoService;
import com.dahua.messagesyncsh.service.FaultService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

@Slf4j
@Service
public class FaultServiceImpl implements FaultService {

    @Resource
    private FaultInfoService faultInfoService;
    @Resource
    private SensorDhMapper sensorDhMapper;
    @Resource
    private PartGroupMapper partGroupMapper;
    @Resource
    private FaultMapper faultMapper;

    @Override
    public void insert(EqStatusMessage eqStatusMessage, EquipmentInfo equipmentInfo) {
        FaultInfoQueryCriteria faultInfoQueryCriteria = new FaultInfoQueryCriteria();
        if (equipmentInfo.getBk2().equals("ZT")) {
            faultInfoQueryCriteria.setDia_Rlt("SZW-" + eqStatusMessage.getAlarmCode());
        } else {
            faultInfoQueryCriteria.setDia_Rlt("SFW-" + eqStatusMessage.getAlarmCode());
        }
        List<FaultInfo> faultInfos = faultInfoService.enquire(faultInfoQueryCriteria);
        if (faultInfos == null || faultInfos.isEmpty()) {
            log.error("未找到故障信息 {}", faultInfoQueryCriteria.getDia_Rlt());
            return;
        }
        FaultInfo faultInfo = faultInfos.get(0);
        SensorDh sensorDh = sensorDhMapper.selectById(faultInfo.getPartCode());
        PartGroup partGroup = partGroupMapper.selectById(sensorDh.getPartGroupId());
        Fault fault = Fault.builder()
                .dataServerId(6)
                .dataId(-20)
                .Station_ID(equipmentInfo.getStationId())
                .Time(Timestamp.valueOf(eqStatusMessage.getTimeStamps()))
                .Eq_ID(equipmentInfo.getId())
                .Dia_Rlt(faultInfo.getDia_Rlt())
                .Remark(partGroup.getBk1())
                .EndTime(Timestamp.valueOf(eqStatusMessage.getTimeStamps()))
                .Period(20)
                .TotalTimes(1)
                .isFixed(0)
                .isChecked(0)
                .lineId(equipmentInfo.getLineId())
                .diaLevel(faultInfo.getDia_Level())
                .deptId(91)
                .isReceived(0)
                .bk1id(0)
                .bk2id(0)
                .bk1(faultInfo.getFaultClass())
                .bk2(equipmentInfo.getBk2())
                .isSync(0)
                .equipmentBrandId(equipmentInfo.getBrandId())
                .partGroupId(sensorDh.getPartGroupId())
                .sensorId(sensorDh.getId())
                .sensorType(sensorDh.getSensorType())
                .bk5(equipmentInfo.getBk5())
                .isHandle(1)
                .build();
        faultMapper.insert(fault);
    }
}
